package cn.ncupyh.comptest;

//求数组的平均值
public class CalArraysAverage {
    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4,5};
        printArray(arr1);
        double ave = calArraysAverage(arr1);
        System.out.println(ave);
        int sum = sum(arr1);
        System.out.println(sum);
        transform(arr1);
        System.out.println(java.util.Arrays.toString(arr1));
//        创建一个 int 类型的数组, 元素个数为 100, 并把每个元素依次设置为 1 - 100
        int[] arr2 = new int[100];
        fillArray(arr2);
        printArray(arr2);
    }

    private static void fillArray(int[] arr2) {
        for (int i = 0; i < arr2.length; i++) {
            arr2[i] = i+1;
        }
    }

    //打印数组
//    实现一个方法 printArray, 以数组为参数, 循环访问数组中的每个元素, 打印每个元素的值.
    public static void printArray(int[] arr1) {
        String ret = "[";
        for (int i = 0; i < arr1.length; i++) {
            if(i < arr1.length-1){
                ret += arr1[i]+",";
                continue;
            }
            ret += arr1[i];
        }
        System.out.println(ret+"]");
    }

    private static void transform(int[] arr1) {
        for (int i = 0; i < arr1.length; i++) {
            arr1[i] *= 2;
        }
    }

    private static int sum(int[] arr1) {
        int sum = 0;
        for (int i = 0; i < arr1.length; i++) {
            sum += arr1[i];
        }
        return sum;
    }

    private static double calArraysAverage(int[] arr1) {
        int sum = 0;
        for (int i = 0; i < arr1.length; i++) {
            sum += arr1[i];
        }
        return sum /arr1.length;
    }
}
